package at.fpmedv.eclipse.helma.core;

import helma.framework.core.Prototype;
import helma.framework.core.PrototypeDecorator;
import helma.framework.repository.Resource;

import java.util.ArrayList;
import java.util.Comparator;

import org.eclipse.jface.preference.IPreferenceStore;

import at.fpmedv.eclipse.helma.preferences.HelmaConstants;
import at.fpmedv.eclipse.helma.util.ResourceContainer;

/**
 * represents an Helma Prototype in the tree view
 * 
 * @author fpm
 * 
 */
public class HelmaPrototypeTreeObject implements ITreeParent {
	
	/**
	 * The helma prototype object
	 */
	private PrototypeDecorator proto;
	
	private Prototype originalPrototype;
	
	/**
	 * parent TreeObject mostly an {@link HelmaAppTreeObject}
	 */
	private ITreeParent parent;
	
	/**
	 * Stores the corresponding Helma parent prototype
	 */
	private HelmaPrototypeTreeObject basePrototype; 

	/**
	 * constructor
	 * 
	 * @param proto helma prototype representation
	 * @param parent parent always an app
	 */
	public HelmaPrototypeTreeObject(Prototype proto, ITreeParent parent) {
		this.proto = new PrototypeDecorator(proto);
		this.parent = parent;
	}

	/**
	 * just a setter for the Helma parent prototype
	 * 
	 * @param basePrototype the Treeobject representation of the parent prototype
	 */
	protected void setBasePrototype(HelmaPrototypeTreeObject basePrototype) {
		this.basePrototype = basePrototype;
	}

	/**
	 * if this node has always children because every Prototype in a HopObject
	 */
	public boolean hasChildren() {
		return true;
	}

	/**
	 * returns list of children
	 */
	public ITreeObject[] getChildren() {
		IPreferenceStore store = HelmaCorePlugin.getDefault().getPreferenceStore();
		ArrayList<ITreeObject> result = new ArrayList<ITreeObject>();
		// list children

		// list properties
		if (store.getBoolean(HelmaConstants.P_GROUP_PROPERTIES)) {
			result.add((ITreeObject) new PropertyGroupTreeObject(proto, this));
		} else {
			result.addAll(proto.getProperties(this));
		}
		// list functions
		if (store.getBoolean(HelmaConstants.P_GROUP_FUNCTIONS)) {
			result.add((ITreeObject) new FunctionGroupTreeObject(proto, this));
		} else {
			result.addAll(proto.getFunctions(this));
		}
		// list skins
		if (store.getBoolean(HelmaConstants.P_GROUP_SKINS)) {
			result.add((ITreeObject) new SkinGroupTreeObject(proto, this));
		} else {
			result.addAll(proto.getSkins(this));
		}

		return (ITreeObject[]) result.toArray(new ITreeObject[result.size()]);
	}

	public Object getAdapter(Class key) {
		return null;
	}

	public String getName() {
		String result = "";
		if (basePrototype != null && !basePrototype.getName().equals("HopObject")) {
			result = ":" + basePrototype.getName();
		}
		return proto.getName() + result;
	}

	public String getRawName() {
		return proto.getName();
	}

	public ITreeParent getParent() {
		return parent;
	}

	@Override
	public String toString() {
		return getName();
	}

	public ArrayList<Resource> getResources() {
		return proto.getAllResources();
	}

	public int compare(ITreeObject element, Comparator comparator) {
		return comparator.compare(this.getName(), element.getName());
	}

	public ResourceContainer getResource() {
		return null;
	}
}
